package com.zhongxi.guoyuan.adapter.coinShop;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.coinStore.CoinOrderData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;
import com.zhongxi.guoyuan.utils.MyTextUtils;
import com.zhongxi.guoyuan.view.coinShopping.CoinCommentActivity;

import java.util.List;

/**
 * Created by Administrator on 2016/12/15.
 */

public class CoinOrderAdapter extends BaseAdapter {

    private Context context;
    private List<CoinOrderData> data;
    private TextView tvNum;
    private EditText etTips;
    private Handler handler;

    public CoinOrderAdapter(Context context, List<CoinOrderData> data, Handler handler) {
        this.context = context;
        this.data = data;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_person_coin_order, parent, false);
            holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
            holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            holder.tvPhone = (TextView) convertView.findViewById(R.id.tvPhone);
            holder.tvMoney = (TextView) convertView.findViewById(R.id.tvMoney);
            holder.tvOrderTime = (TextView) convertView.findViewById(R.id.tvOrderTime);
            holder.tvComment = (TextView) convertView.findViewById(R.id.tvComment);
            holder.tvComplain = (TextView) convertView.findViewById(R.id.tvComplain);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        final CoinOrderData list = data.get(position);
        if(null!=list.getShopthumbnail()&&!"".equals(list.getShopthumbnail())){
            Glide.with(context).load(Url.IMG_URL+list.getShopthumbnail()).placeholder(R.mipmap.iv_eg).transform(new GlideRoundTransform(context, 20)).into(holder.ivImg);
        }
        if(null!=list.getShopname()&&!"".equals(list.getShopname())){
            holder.tvName.setText(list.getShopname());
        }else{
            holder.tvName.setText("");
        }
        if(null!=list.getShopphone()&&!"".equals(list.getShopphone())){
            holder.tvPhone.setText("联系商家："+list.getShopphone());
        }else{
            holder.tvPhone.setText("联系商家：******");
        }
        if(null!=list.getPrice()&&!"".equals(list.getPrice())){
            holder.tvMoney.setText("付款金额："+list.getPrice()+"银币");
        }else{
            holder.tvMoney.setText("付款金额：***银币");
        }
        if(null!=list.getCtime()&&!"".equals(list.getCtime())){
            holder.tvOrderTime.setText("付款时间："+list.getCtime());
        }else{
            holder.tvOrderTime.setText("付款时间：***");
        }

        if("1".equals(list.getStatus())){
            holder.tvComment.setText("已评价");
            holder.tvComment.setText("我投诉");
        }else if("2".equals(list.getStatus())){
            holder.tvComment.setText("去评价");
            holder.tvComplain.setText("已投诉");
        }else{
            holder.tvComment.setText("去评价");
            holder.tvComplain.setText("我投诉");
        }

        final int pos = position;
        //评论
        holder.tvComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("1".equals(list.getStatus())){
                    Toast.makeText(context, "您已经评价过了！", Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = new Intent(context, CoinCommentActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("info", data.get(pos));
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            }
        });

        //投诉
        holder.tvComplain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("2".equals(list.getStatus())){
                    Toast.makeText(context, "您已经投诉过了！", Toast.LENGTH_SHORT).show();
                }else {
                    View view = ((Activity) context).getLayoutInflater().inflate(R.layout.item_complain, null);
                    final Dialog dialog = new AlertDialog.Builder(context).create();
                    dialog.show();
                    dialog.getWindow().setContentView(view);

                    dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

                    ImageView ivCancel = (ImageView) view.findViewById(R.id.ivCancel);
                    etTips = (EditText) view.findViewById(R.id.etTips);
                    tvNum = (TextView) view.findViewById(R.id.tvNum);
                    Button btSubmit = (Button) view.findViewById(R.id.btSubmit);
                    ivCancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                    etTips.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {

                        }

                        @Override
                        public void afterTextChanged(Editable content) {
                            MyTextUtils.TextChangeUtils(context, tvNum, content);
                        }
                    });
                    btSubmit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String content = etTips.getText().toString().trim();
                            Message msg = Message.obtain();
                            msg.arg1 = pos;
                            msg.obj = content;
                            handler.sendMessage(msg);
                            dialog.dismiss();
                        }
                    });
                }
            }
        });
        return convertView;
    }

    class ViewHolder{
        private ImageView ivImg;
        private TextView tvName;
        private TextView tvPhone;
        private TextView tvMoney;
        private TextView tvOrderTime;
        private TextView tvComment;
        private TextView tvComplain;
    }
}
